package unit9_9.pojo;

import java.net.ConnectException;
import java.sql.*;

public class MySql {
    public static void main(String[] args) {


        try {
            // 1.加载数据库驱动类
            Class.forName("com.mysql.cj.jdbc.Driver");
            System.out.println("数据库驱动加载成功");

            // 2.创建连接
            //格式为jdbc:mysql:
            // 127.0.0.1:3306/数据库名称?useSSL=true&characterEncoding=utf-8&user=账号名&password=密码
            Connection connection = DriverManager.getConnection
                    ("jdbc:mysql://localhost:3306/school?useSSL=true&characterEncoding=utf-8&serverTimezone=GMT&user=root&password=123456");
            System.out.println("创建连接成功");
            Statement statement=connection.createStatement();
            String sql;
            sql="select * from stu";
            ResultSet ret=statement.executeQuery(sql);
            while(ret.next()){
                Integer sno=ret.getInt("sno");
                String name=ret.getString("sname");
                String sex =ret.getString("ssex");

                System.out.print("no:"+sno);
                System.out.print(",name:"+name);
                System.out.println(",sex"+sex);
            }
            connection.close();

        }
        catch (ClassNotFoundException cnfe) {
            cnfe.printStackTrace();
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        }










    }

}
